<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Answer extends Model
{
    // 添加回答 api
    public function add()
    {
        /* 检查用户是否登录 */
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }
       /* 检查参数中是否存在question_id 和 content*/
        if (!rq('question_id') || !rq('content')) {
            return ['status' => 0, 'msg' => 'question_id and content are required'];
        }
        // 检查问题是否存在(通过 question_id 字段 进行查找 )
        $question = question_ins()->find(rq('question_id'));
        if (!$question) return ['status' => 0, 'msg' => 'question not exists'];
        /*检查是否重复回答*/
        $answered = $this
            ->where(['question_id' => rq('question_id'), 'user_id' => session('user_id')])
            ->count();
        if ($answered) {
            return ['status' => 0, 'msg' => 'duplicate answers'];
        }
        /*保存数据*/
        $this->content = rq('content');
        $this->question_id = rq('question_id');
        $this->user_id = session('user_id');

        return $this->save() ?
            ['status' => 1, 'id' => $this->id] :
            ['status' => 0, 'msg' => 'db insert failed'];
    }

    /*更新回答 api*/
    public function change()
    {
        /* 检查用户是否登录 */
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }
        /* 检查参数中是否存在 id 和 content */
        if (!rq('id') || !rq('content')) {
            return ['status' => 0, 'msg' => 'id and content are required'];
        }
        // 通过 id 查找 当前 回答的数据
        $answer = $this->find(rq('id'));
        // 判断当前回答的用户id 是否 是 当前 登录的用户 ID
        if ($answer->user_id != session('user_id')) {
            return ['status' => 0, 'msg' => 'permission denied'];
        }
        // 更新 回答 内容为传递过来的参数
        $answer->content = rq('content');
        return $answer->save() ?
            ['status' => 1] :
            ['status' => 0, 'msg' => 'db update failed'];
    }

    // 查看回答 api
    public function read()
    {
        /* 检查参数中是否存在 id 和 question_id */
        if (!rq('id') && !rq('question_id')) {
            return ['status' => 0, 'msg' => 'id or question_id is required'];
        }

        if (rq('id')) {
            // 查看单个回答
            $answer = $this->find(rq('id'));
            if (!$answer) {
                return ['status' => 0, 'msg' => 'answer not exists'];
            } else {
                return ['status' => 1, 'data' => $answer];
            }
        }

        // 在查看回答前，检查问题是否存在
        if (!question_ins()->find(rq('question_id'))) {
            return ['status' => 0, 'msg' => 'question not exists'];
        }

        // 查看同一问题下的所有回答
        $answers = $this
            ->where('question_id', rq('question_id'))
            ->get()
            ->keyBy('id');

        return ['status' => 1, 'data' => $answers];
    }

    // 删除回答 api
    public function remove()
    {
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }

        if (!rq('id')) {
            return ['status' => 0, 'msg' => 'id is required'];
        }

        $answer = $this->find(rq('id'));
        if (!$answer) {
            return ['status' => 0, 'msg' => 'answer not exists'];
        }

        if ($answer->user_id != session('user_id')) {
            return ['status' => 0, 'msg' => 'permission denied'];
        }

        return $answer->delete() ?
            ['status' => 1] :
            ['status' => 0, 'msg' => 'db delete failed'];
    }

    public function users()
    {
        return $this
            ->belongsToMany('App\User')
            ->withPivot('vote')
            ->withTimestamps();
    }

    // 投票 api
    public function vote()
    {
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }

        if (!rq('id') || !rq('vote')) {
            return ['status' => 0, 'msg' => 'id and vote are required'];
        }

        $answer = $this->find(rq('id'));
        if (!$answer) return ['status' => 0, 'msg' => 'answer not exists'];

        // 1是赞同 2是反对
        $vote = rq('vote') <=1 ? 1 : 2;
        // 检查此用户是否在相同问题下投过票， 如果投过票就删除投票
        $answer->users()
            ->newPivotStatement()
            ->where('user_id', session('user_id'))
            ->where('answer_id', rq('id'))
            ->delete();
        /*在连接表中增加数据*/
        $answer->users()->attach(session('user_id'), ['vote' => $vote]);
        return ['status' => 1];
    }
}
